package com.bw.utils;

import com.bw.req.OrderPri;
import net.sf.excelutils.ExcelUtils;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.core.io.ClassPathResource;

import javax.servlet.http.HttpServletResponse;
import java.io.InputStream;
import java.util.List;

public class ExportExcel {
    //name为生成的表名   list为查询的列表  path为文件路径如：xls/linkman.xls
    public static void exportExcel(HttpServletResponse response, List list,String name,String path) throws Exception {
        response.setHeader("Content-Type","application/vnd.ms-excel;charset=UTF-8");
        response.setHeader("Content-Disposition","attachment;filename="+new String(name.getBytes(),"iso-8859-1")+".xls");
        ClassPathResource classPathResource = new ClassPathResource(path);//定位到具体位置
        InputStream inputStream = classPathResource.getInputStream();//excel模板的流获取
        ExcelUtils.addValue("list",list);
        ExcelUtils.export(inputStream,ExcelUtils.getContext(),response.getOutputStream());//第一个参数：模板的流,第三个参数：输出到哪
    }

    public static void exportExcel2(HttpServletResponse response, OrderPri orderPri, String name, String path) throws Exception {
        response.setHeader("Content-Type","application/vnd.ms-excel;charset=UTF-8");
        response.setHeader("Content-Disposition","attachment;filename="+new String(name.getBytes(),"iso-8859-1")+".xls");
        ClassPathResource classPathResource = new ClassPathResource(path);//定位到具体位置
        InputStream inputStream = classPathResource.getInputStream();//excel模板的流获取
        ExcelUtils.addValue("customer",orderPri.getCustomer());
        ExcelUtils.addValue("linkman",orderPri.getLinkman());
        ExcelUtils.addValue("order",orderPri.getOrder());
        ExcelUtils.addValue("orderItemList",orderPri.getOrderItemList());
        ExcelUtils.export(inputStream,ExcelUtils.getContext(),response.getOutputStream());//第一个参数：模板的流,第三个参数：输出到哪
    }
}
